package cn.lijian.goods.order.service;

import java.sql.SQLException;

import cn.itcast.jdbc.JdbcUtils;
import cn.lijian.goods.order.dao.OrderDao;
import cn.lijian.goods.order.domain.Order;
import cn.lijian.goods.pager.PageBean;

public class OrderService {
	
	private OrderDao orderDao=new OrderDao();
	/**
	 * 查询订单状态
	 * @param oid
	 * @return
	 * @author lijian
	 */
	public int findStatus(String oid){
		try {
			return orderDao.findStatus(oid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 查询订单状态
	 * @param oid
	 * @return
	 * @author lijian
	 */
	public void updateStatus(String oid,int status){
		try {
			orderDao.updateStatus(oid,status);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 加载订单
	 * @param oid
	 * @return
	 * @author lijian
	 */
	public Order load(String oid){
		
		try {
			JdbcUtils.beginTransaction();
			Order order= orderDao.load(oid);
			JdbcUtils.commitTransaction();
			return order;
		} catch (SQLException e) {
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 生成订单
	 * @param order
	 * @author lijian
	 */
	public void createOrder(Order order){
		try {
			JdbcUtils.beginTransaction();
			orderDao.add(order);
			JdbcUtils.commitTransaction();
		} catch (SQLException e) {
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			throw new RuntimeException(e);
		}
	} 
	
	/**
	 * 我的订单
	 * @param uid
	 * @param pcS
	 * @return
	 * @author lijian
	 */
	public PageBean<Order>myOrders(String uid,int pc){
		try {
			JdbcUtils.beginTransaction();
			PageBean<Order>pb=orderDao.findByUser(uid, pc);
			JdbcUtils.commitTransaction();
			return pb;
		} catch (SQLException e) {
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			throw new RuntimeException(e);
		}
	}
	/**
	 * 按订单状态查询
	 * @param status
	 * @param pc
	 * @return
	 * @author lijian
	 */
	public PageBean<Order>findByStatus(int status,int pc){
		try {
			JdbcUtils.beginTransaction();
			PageBean<Order>pb=orderDao.findByStatus(status, pc);
			JdbcUtils.commitTransaction();
			return pb;
		} catch (SQLException e) {
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 查询所有
	 * @param pc
	 * @return
	 * @author lijian
	 */
	public PageBean<Order>findAll(int pc){
		try {
			JdbcUtils.beginTransaction();
			PageBean<Order>pb=orderDao.findAll(pc);
			JdbcUtils.commitTransaction();
			return pb;
		} catch (SQLException e) {
			try {
				JdbcUtils.rollbackTransaction();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			throw new RuntimeException(e);
		}
	}

}
